package top.jacktgq.hsp.nio.buffer_channel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @Author CandyWall
 * @Date 2020/11/11--12:06
 * @Description 拷贝文件，用transferFrom()方法
 *                  将file03.txt中的内容拷贝到file04.txt中
 */
public class NIOFileChannel04 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //创建相关的流
            fis = new FileInputStream(new File("tmp/file03.txt"));
            fos = new FileOutputStream(new File("tmp/file04.txt"));

            //获取各个流对应的FileChannel
            FileChannel sourceChannel = fis.getChannel();
            FileChannel destChannel = fos.getChannel();

            //使用transFrom()方法完成拷贝
            destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
